#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 3e5 + 5;

int n, m;
int a[N];
void p_sort(int l, int r) {
  if (l == r) return;
  int mid = (l + r) >> 1;
  int mn = l;
  rep(i, l, r) if (a[mn] > a[i]) mn = i;
  if (mn > mid) reverse(a + l, a + r + 1);
  p_sort(l, mid), p_sort(mid + 1, r);
}
void solve() {
  cin >> n;
  n = 1 << n;
  rep(i, 1, n) cin >> a[i];
  p_sort(1, n);
  rep(i, 1, n) cout << a[i] << " \n"[i == n];
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}